import request from "@/utils/http";
import {ResponseResult} from "@/api/common-types.ts";
import {MusicListMapType, MusicListType, MusicSongType, MusicSongYelpVOType} from "@/api/list/type.ts";
import {AxiosProgressEvent} from "axios";

/**
 * 获取用户歌单列表
 */
export function getUserMusicListApi(): Promise<ResponseResult<MusicListMapType>> {
    return request({
        url: "/list/getUserSongList",
        method: "get",
        data: new Date().getTime(),
        headers: {
            "Content-Type": "application/json;charset=utf-8",
        },
    });
}

/**
 * 根据歌单id获取歌曲列表
 */
export function getSongsApi(data: MusicListType): Promise<ResponseResult<MusicSongType[]>> {
    return request({
        url: "/list/getSongList",
        method: "get",
        params: {
            listId: data.listId,
        },
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
}

/**
 * 根据歌单id获取歌曲列表
 */
export function getLoveMusicListApi(): Promise<ResponseResult<MusicSongType[]>> {
    return request({
        url: "/list/findLoveMusicList",
        method: "get",
        headers: {
            "Content-Type": "multipart/form-data",
        },
    });
}

/**
 * 更新用户歌曲点评
 */
export function updateSongYelpApi(data: MusicSongYelpVOType): Promise<ResponseResult<string>> {
    return request({
        url: "/song/updateSongYelp",
        method: "post",
        data: JSON.stringify(data),
        headers: {
            "Content-Type": "application/json;charset=utf-8",
        },
    });
}

/**
 * @Description: 文件封面上传
 * @Author: sxgan
 * @Date: 2025/7/6 19:07
 **/
export function updateMusicListCoverApi(
    listId: number,
    file: File
): Promise<ResponseResult<string>> {
    const formData = new FormData();
    formData.append("listId", String(listId)); // 确保数字转换为字符串
    formData.append('file', file);

    return request({
        url: "/list/updateSongCover",
        method: "post",
        data: formData,
        // 移除手动设置的 Content-Type，让浏览器自动设置 multipart/form-data
        headers: {
            "Content-Type": "multipart/form-data",
        },
        // 添加上传进度监控（可选）
        onUploadProgress: (progressEvent: AxiosProgressEvent) => {
            if (progressEvent.total) {
                const percent = Math.round(
                    (progressEvent.loaded * 100) / progressEvent.total
                );
                console.log(`上传进度: ${percent}%`);
            }
        },
    });
}